package com.example.imoocmusicdemo.utils;

import android.content.Context;
import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class DataUtils {

    /**
     * 读取资源文件中的数据
     * */
    public static String getJasonFromAssets(Context context,String fileName){
        /**
         * StringBuilder 存放读取出来的数据
         * AssetsManager 资源管理器，Open 方法打开指定资源文件
         * InputStreamReader 字节到字符的桥接器，BufferReader 存放读取字符的缓冲区
         * 循环利用 BufferReader 和 readLine 方法读取每一行数据，放置在StringBuilder里
         * 返回读取数据
         * */

        StringBuilder stringBuilder = new StringBuilder();
        AssetManager assetManager = context.getAssets();
        try {
            InputStream inputStream = assetManager.open(fileName);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while((line = bufferedReader.readLine()) != null){
                stringBuilder.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();

    }

}
